﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CloudChartLib
{
    /// <summary>
    /// 颜色扩展
    /// </summary>
    public class ColorExpand
    {
        /// <summary>
        /// 颜色列表
        /// </summary>
        public RGB[] Colors { get; private set; }


        public ColorExpand()
        {
            string[] colors = new string[] { "#072FFA", "#0779FA", "#07C4FA", "#07DFD0", "#07FAA7", "#79FA57", "#ECFA07", "#F3D307", "#FAAD07", "#F67207", "#FA1207" };
            int colorNum = 150;
            int color_len = colors.Length;
            byte[] rs = new byte[color_len];
            byte[] gs = new byte[color_len];
            byte[] bs = new byte[color_len];
            for (int i = color_len - 1; i >= 0; i--)
            {
                ParseColor(colors[i], out rs[i], out gs[i], out bs[i]);
            }

            DiscreteExpand de = new DiscreteExpand(color_len, colorNum);

            Colors = new RGB[colorNum];
            for (int i = 0; i < colorNum; i++)
            {
                de.GetSegmentInfo(i, out int index, out double dist);
                byte r = (byte)SingleLinear(dist, 1, rs[index], rs[index + 1]);
                byte g = (byte)SingleLinear(dist, 1, gs[index], gs[index + 1]);
                byte b = (byte)SingleLinear(dist, 1, bs[index], bs[index + 1]);
                Colors[i] = new RGB(r, g, b);
            }
        }

        /// <summary>
        /// 创建扩展颜色列表
        /// </summary>
        /// <param name="colors">颜色字符串列表，格式为#ff00cc</param>
        /// <param name="colorNum">目标颜色数</param>
        public ColorExpand(string[] colors, int colorNum)
        {
            int color_len = colors.Length;
            byte[] rs = new byte[color_len];
            byte[] gs = new byte[color_len];
            byte[] bs = new byte[color_len];
            for (int i = color_len - 1; i >= 0; i--)
            {
                ParseColor(colors[i], out rs[i], out gs[i], out bs[i]);
            }

            DiscreteExpand de = new DiscreteExpand(color_len, colorNum);

            Colors = new RGB[colorNum];
            for (int i = 0; i < colorNum; i++)
            {
                de.GetSegmentInfo(i, out int index, out double dist);
                byte r = (byte)SingleLinear(dist, 1, rs[index], rs[index + 1]);
                byte g = (byte)SingleLinear(dist, 1, gs[index], gs[index + 1]);
                byte b = (byte)SingleLinear(dist, 1, bs[index], bs[index + 1]);
                Colors[i] = new RGB(r, g, b);
            }
        }

        private void ParseColor(string str, out byte R, out byte G, out byte B)
        {
            R = Convert.ToByte(str.Substring(1, 2), 16);
            G = Convert.ToByte(str.Substring(3, 2), 16);
            B = Convert.ToByte(str.Substring(5, 2), 16);
        }

        private double SingleLinear(double m, double X, double V1, double V2)
        {
            return m * (V2 - V1) / X + V1;
        }
    }
}
